package cn.dt70.service.impl;

import cn.dt70.dao.aqbPacket.AqbPacketMapper;
import cn.dt70.pojo.AqbPacket;
import cn.dt70.service.AqbPacketService;
import cn.dt70.utils.PageSupport;
import com.sun.deploy.ui.AppInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("AqbPacketService")
public class AqbPacketServiceImpl implements AqbPacketService {

    private static final Logger LOGGER = Logger.getLogger(AqbPacketServiceImpl.class);

    @Resource
    private AqbPacketMapper aqbPacketMapper;

    @Override
    public boolean addhongbao(AqbPacket aqbPacket) {
        int row=0;
        try {
            row=aqbPacketMapper.newManBao(aqbPacket);
            if(row==1){
                LOGGER.debug("aqbPacketMapper.newManBao 新增成功");
                return true;
            }else{
                LOGGER.debug("aqbPacketMapper.newManBao 新增失败");
                return false;
            }
        } catch (Exception e) {
            LOGGER.error("aqbPacketMapper.newManBao 新增异常"+e.getMessage());
            return false;
        }
    }

    @Override
    public boolean updateHongbaoStatus(Integer honeId ,Integer status) {
        int row=0;
        try {
            row=aqbPacketMapper.udpPacketStatus(status,honeId);
            if(row==1){
                LOGGER.debug("aqbPacketMapper.udpPacketStatus 修改成功");
                return true;
            }else{
                LOGGER.debug("aqbPacketMapper.udpPacketStatus 修改失败");
                return false;
            }
        } catch (Exception e) {
            LOGGER.error("aqbPacketMapper.udpPacketStatus 修改异常"+e.getMessage());
            return false;
        }
    }

    @Override
    public List<AqbPacket> getAllAqbPacketByStatus(PageSupport ps, Integer userId, Integer status) {
        List<AqbPacket> aqbPacketList = new ArrayList<AqbPacket>();
        try {
            //更新总条数
            int count = aqbPacketMapper.getHongbaoListCount(userId,status);
            ps.setTotalCount(count);

            int s = (ps.getCurrentPageNo()-1)*ps.getPageSize();
            int a = ps.getPageSize();

            aqbPacketList = aqbPacketMapper.getHongbaoList(s,a,userId,status);
            if(null == aqbPacketList ||  ("").equals(aqbPacketList)){
                LOGGER.debug("aqbPacketMapper.getHongbaoList 查询为空》》》");
            }else{
                LOGGER.debug("aqbPacketMapper.getHongbaoList 查询成功》》》");
            }
        } catch (Exception e) {
//            e.printStackTrace();
            LOGGER.error("aqbPacketMapper.getHongbaoList 查询异常》》》"+e.getMessage());
        }
        return aqbPacketList;
    }

    @Override
    public AqbPacket getHongbaoXinxi(Integer honeId) {
        AqbPacket aqbPacket=new AqbPacket();
        try {
            aqbPacket=aqbPacketMapper.getHongbaoInformation(honeId);
            if(null == aqbPacket){
                LOGGER.debug("appInfoMapper.getHongbaoInformation 查询失败");
                aqbPacket=new AqbPacket();
            }else{
                LOGGER.debug("appInfoMapper.getHongbaoInformation 查询成功");
            }
        } catch (Exception e) {
            LOGGER.error("appInfoMapper.getHongbaoInformation 查询异常");
            aqbPacket=new AqbPacket();
        }
        return aqbPacket;
    }

    @Override
    public int sumPacket(Integer userId) {
        int sum = 0;
        try {
            sum = aqbPacketMapper.sumPacket(userId);
            System.out.println("可用红包的总额为"+sum);
        } catch (Exception e) {
            LOGGER.error("aqbPacketMapper.sumPacket 计算红包总数异常"+e.getMessage());
        }
        return sum;
    }

    @Override
    public List<AqbPacket> listHongbao(Integer userId, Integer useCondition) {
        List<AqbPacket> aqbPacketList = new ArrayList<AqbPacket>();
        try {
            aqbPacketList = aqbPacketMapper.useHongbao(userId,useCondition);
            if(null == aqbPacketList ||  ("").equals(aqbPacketList)){
                LOGGER.debug("aqbPacketMapper.useHongbao 查询为空》》》");
            }else{
                LOGGER.debug("aqbPacketMapper.useHongbao查询成功》》》");
            }
        } catch (Exception e) {
//            e.printStackTrace();
            LOGGER.error("aqbPacketMapper.useHongbao查询异常》》》"+e.getMessage());
        }
        return aqbPacketList;
    }

    @Override
    public List<AqbPacket> listApacket(Integer userId,Integer status) {
        List<AqbPacket> aqbPacketList = new ArrayList<AqbPacket>();
        try {
            aqbPacketList = aqbPacketMapper.listAqbpacket(userId,status);
            if(null == aqbPacketList ||  ("").equals(aqbPacketList)){
                LOGGER.debug("aqbPacketMapper.listAqbpacket 查询为空》》》");
            }else{
                LOGGER.debug("aqbPacketMapper.listAqbpacket查询成功》》》");
            }
        } catch (Exception e) {
//            e.printStackTrace();
            LOGGER.error("aqbPacketMapper.listAqbpacket查询异常》》》"+e.getMessage());
        }
        return aqbPacketList;
    }

    //后台查询红包
    @Override
    public List<AqbPacket> getPacketInfo(PageSupport ps) {
        List<AqbPacket> aqbPackets = new ArrayList<>();
        try {
            ps.setTotalCount(aqbPacketMapper.qryPackCount());
            aqbPackets = aqbPacketMapper.aqbPacketList((ps.getCurrentPageNo()-1)*ps.getPageSize(),ps.getPageSize());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aqbPackets;
    }

    @Override
    public List<AqbPacket> getjiaxiInfo(PageSupport ps) {
        List<AqbPacket> jiaxiInfo = new ArrayList<>();
        try {
            ps.setTotalCount(aqbPacketMapper.qryjiaxiCount());
            jiaxiInfo = aqbPacketMapper.jiaxiList((ps.getCurrentPageNo()-1)*ps.getPageSize(),ps.getPageSize());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jiaxiInfo;
    }
}
